package com.example.springcloud.controller;


import com.example.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class UserConsumerController {
//    理解：消费者没有service层
//    RestTemplate，供我们直接调用就可以了
//    (url, 尸体：Map,Class<T> responseType)
    @Autowired
    private RestTemplate restTemplate;

    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-USER";


    @RequestMapping("/consumer/user/add")
    public boolean add(User user){
        return restTemplate.postForObject(REST_URL_PREFIX+"/user/add",user, Boolean.class);
    }

    @RequestMapping("/consumer/user/get/{id}")
    public User get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/user/get/"+id,User.class);
    }

    @RequestMapping("/consumer/user/list")
    public List<User> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/user/list",List.class);
    }
}
